package com.feisuanyz.service.impl;

import com.feisuanyz.dto.RestResult;
import com.feisuanyz.dto.TicketQuery;
import com.feisuanyz.entity.OperationLog;
import com.feisuanyz.entity.TicketManagement;
import com.feisuanyz.repository.OperationLogRepository;
import com.feisuanyz.repository.TicketManagementRepository;
import com.feisuanyz.service.TicketService;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class TicketServiceImpl implements TicketService {

    @Autowired
    private TicketManagementRepository ticketManagementRepository;

    @Autowired
    private OperationLogRepository operationLogRepository;

    @Autowired
    private ModelMapper modelMapper;

    @Override
    @Transactional
    public RestResult<Void> createTicket(TicketQuery ticketQuery) {
        try {
            TicketManagement ticketManagement = modelMapper.map(ticketQuery, TicketManagement.class);
            ticketManagement.setCreateTime(new Date());
            ticketManagementRepository.save(ticketManagement);

            OperationLog operationLog = new OperationLog();
            operationLog.setTicketId(ticketManagement.getTicketId());
            operationLog.setOperationType(OperationLog.OperationType.CREATE);
            operationLog.setDetails("Created ticket with ID: " + ticketManagement.getTicketId());
            operationLog.setCreateBy(ticketQuery.getCreateBy());
            operationLog.setCreateTime(new Date());

            operationLogRepository.save(operationLog);

            return new RestResult<>("000000", "调用成功", null);
        } catch (Exception e) {
            log.error("Failed to create ticket", e);
            return new RestResult<>("999999", "系统异常", null);
        }
    }

    @Override
    @Transactional
    public RestResult<Void> editTicket(TicketQuery ticketQuery) {
        try {
            TicketManagement existingTicket = ticketManagementRepository.findById(ticketQuery.getTicketId())
                    .orElseThrow(() -> new RuntimeException("Ticket not found"));

            if (ticketQuery.getEventId() != null) {
                existingTicket.setEventId(ticketQuery.getEventId());
            }
            if (ticketQuery.getAvailableTickets() != null) {
                existingTicket.setAvailableTickets(ticketQuery.getAvailableTickets());
            }
            if (ticketQuery.getRemainingTickets() != null) {
                existingTicket.setRemainingTickets(ticketQuery.getRemainingTickets());
            }
            existingTicket.setUpdateBy(ticketQuery.getUpdateBy());

            ticketManagementRepository.save(existingTicket);

            OperationLog operationLog = new OperationLog();
            operationLog.setTicketId(existingTicket.getTicketId());
            operationLog.setOperationType(OperationLog.OperationType.EDIT);
            operationLog.setDetails("Updated ticket with ID: " + existingTicket.getTicketId());
            operationLog.setCreateBy(ticketQuery.getUpdateBy());
            operationLog.setCreateTime(new Date());

            operationLogRepository.save(operationLog);

            return new RestResult<>("000000", "调用成功", null);
        } catch (Exception e) {
            log.error("Failed to edit ticket", e);
            return new RestResult<>("999999", "系统异常", null);
        }
    }

    @Override
    @Transactional
    public RestResult<Void> deleteTicket(Integer ticketId, Integer deleteBy) {
        try {
            TicketManagement ticketManagement = ticketManagementRepository.findById(ticketId)
                    .orElseThrow(() -> new RuntimeException("Ticket not found"));

            ticketManagementRepository.delete(ticketManagement);

            OperationLog operationLog = new OperationLog();
            operationLog.setTicketId(ticketId);
            operationLog.setOperationType(OperationLog.OperationType.DELETE);
            operationLog.setDetails("Deleted ticket with ID: " + ticketId);
            operationLog.setCreateBy(deleteBy);
            operationLog.setCreateTime(new Date());

            operationLogRepository.save(operationLog);

            return new RestResult<>("000000", "调用成功", null);
        } catch (Exception e) {
            log.error("Failed to delete ticket", e);
            return new RestResult<>("999999", "系统异常", null);
        }
    }
}